package annTrain;

import java.util.HashMap;

import aNN.InputTargetPair;
import aNN.NeuralNetwork;
import aNN.Neuron;
import aNN.NeuronOutputPair;


public class TrainerHelper 
{
	
	public HashMap<Neuron, NeuronOutputPair> CreateInputMap(NeuralNetwork nn, InputTargetPair inputAndTargets)
	{
		HashMap<Neuron, NeuronOutputPair> inputMap = new HashMap<Neuron, NeuronOutputPair>();
		for (int i = 0; i < nn.InputLayer.Neurons.size(); i++)
		{
			Neuron neuron = nn.InputLayer.Neurons.get(i);
			inputMap.put(neuron, new NeuronOutputPair(inputAndTargets.Input[i], inputAndTargets.Input[i]));
		}
		return inputMap;
	}
	
	public int GetTarget(InputTargetPair inputsAndTargets)
	{
		for (int i = 0; i < inputsAndTargets.Target.length; i++)
		{
			if (inputsAndTargets.Target[i] > 0)
				return i;
		}
		return -1;
	}
	
	public double[] GetNeuronOutputPairLastElements(NeuronOutputPair[] array, int num)
	{
		double[] newArray = new double[num];
		for (int i = array.length - num; i < array.length; i++)
		{
			newArray[i - array.length + num] = array[i].OutputValue;
		}
		return newArray;
	}
	
	public int GetCalculatedResult(double[] results)
	{
		int numOuts = 0;
		int outIndex = 0;
		for (int i = 0; i < results.length; i++)
		{
			if (results[i] > 0)
			{
				outIndex = i;
				numOuts++;
			}
			
		}
		if (numOuts == 1)
			return outIndex;
		else
			return -1;
	}
	
	public double[] GetOutputTarget(int numOutput, int outputIndex)
	{
		double[] returnVal = new double[numOutput];
		for (int i = 0; i < numOutput; i++)
		{
			returnVal[i] = outputIndex == i ? 1 : -1;
		}
		return returnVal;
	}
	
}
